import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: 12629
 * Date: 2021/10/16
 * Time: 8:32
 * Description:
 */
public class TestDemo {
    public static void func1(int[] array) {
        array = new int[10];
    }

    public static void func2(int[] array) {
        array[0] = 9;
    }

    /**
     * 将一个数组里面所有的元素扩大2倍！
     * @param array
     */
    public static void func3(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = 2*array[i];
        }
    }

    public static int[] func4(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = 2*array[i];
        }
        return array;
    }

    public static int[] func5(int[] array) {
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = array[i]*2;
        }
        return ret;
    }

    public static String myToString(int[] array ) {
        if(array == null) return "null";
        if(array.length == 0) return "[]";

        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret = ret + array[i];
            if(i != array.length-1) {
                ret = ret+",";
            }
        }
        ret += "]";
        return ret;
    }

    public static int max(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(max < array[i]) {
                max = array[i];
            }
        }
        return max;
    }

    public static double avg(int[] array) {
        int sum = 0;
       /* for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }*/
        for (int val:array) {
            sum += val;
        }
        return sum*1.0/array.length;
    }

    public static int findVal(int[] array,int key) {

        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public static int count = 0;//计数器
    public static int binarySearch(int[] array,int key) {
        int i = 0;
        int j = array.length-1;
        while (i <= j) {
            count++;
            int mid = (i+j)/2;
            if(array[mid] < key) {
                i = mid+1;
            }else if(array[mid] > key) {
                j = mid-1;
            }else {
                return mid;
            }
        }
        return -1;
    }

    public static boolean isUp(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            if(array[i] > array[i+1]) {
                return false;
            }
        }
        return true;
    }

    public static void bubbleSort(int[] array) {
        if(array == null) return;

        for (int i = 0; i < array.length-1; i++) {
            boolean flg = false;

            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;//此时交换了-》没有 完全有序
                }
            }
            if(flg == false) {
                //没有进入if(array[j] > array[j+1]) 语句
                break;
            }
        }
    }

    public static void main(String[] args) {
        int[] array = {1,12,31,4,5};
        System.out.println(Arrays.toString(array));
        bubbleSort(array);
        System.out.println(Arrays.toString(array));

        //System.out.println(isUp(array));

        // Arrays.sort(array);//从小到大进行排序
       // System.out.println(Arrays.binarySearch(array, 14));

        //System.out.println(Arrays.toString(array));//1,2,4,5,31
        //System.out.println(binarySearch(array, 4));
    }

    public static void main3(String[] args) {
        int[] array = new int[10000];//0-9999
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
        System.out.println(binarySearch(array, 9999));
        System.out.println(TestDemo.count);
    }

    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(binarySearch(array, 13));

        //double ret = avg(array);
        //System.out.println(ret);
    }

    public static void main1(String[] args) {
        //int[] array = {1,2,3,4,5};
        /*System.out.println(myToString(null));
        int[] array=null;
        System.out.println(myToString(array));
        System.out.println(Arrays.toString(array));
        */
        //String s1 = null;
        //System.out.println(s1.length());
        String s2 = "";
        System.out.println(s2.length());
        int[] array = {1,2,3,4,5};
        System.out.println(Arrays.toString(array));


       /* String ret = "[";
        ret + array[0]+","*/


        /*int[] v = func5(array);
        System.out.println(Arrays.toString(v));


        int[] ret = func4(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));*/
    }
}
